package com.itheima.pattern.responsibility.jdk_filter;

import java.util.ArrayList;
import java.util.List;

/**
 * 模拟实现过滤器链
 */
public class FilterChain {
    private List<Filter> filters=new ArrayList<>();
    private int index=0;

    //链式调用
    public FilterChain addFilter(Filter filter){
        this.filters.add(filter);
        return this;
    }


    public void doFilter(Request req, Response resp) {
        if(index==filters.size()){
            return;
        }
        Filter filter = filters.get(index);
        index++;
        filter.doFilter(req,resp,this);

    }
}
